package yhp.dao.impl;

import yhp.dao.UserDao;
import yhp.util.DruidUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends DruidUtil implements UserDao {
    @Override
    public int sum() {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select count(praiseStatus) from user where praiseStatus=1";
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    @Override
    public boolean findByUserName(String userName) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from user where userName=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                return true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return false;
    }

    @Override
    public void changePraiseStatus(String userName,int praiseStatus) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "update user set praiseStatus=? where userName=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,praiseStatus);
            preparedStatement.setString(2,userName);
            preparedStatement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }

    }

    @Override
    public int getPraiseStatus(String userName) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select praiseStatus from user where userName=?";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return 0;
    }

    @Override
    public boolean insert(String userName, String userPassword) {
        Connection connection = getConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "insert into user values(?,?,?)";
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,userName);
            preparedStatement.setString(2,userPassword);
            preparedStatement.setInt(3,0);//默认新用户的点赞状态为0
            int i = preparedStatement.executeUpdate();
            return i>0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(connection,preparedStatement,resultSet);
        }
        return false;
    }

}
